/*
 * @author Javier A. Ramírez M.
 * jaramirez85@yahoo.es
 */
package com.jav;

import com.jav.data.InvMovilData;
import com.jav.per.PersisCaptura;
import com.jav.per.Persistencia;
import com.sun.lwuit.Button;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Label;
import com.jav.componentes.textfield.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.util.Resources;
import javax.microedition.rms.RecordEnumeration;

public class ProductoContainer extends Container {

    private Button bt_buscar;
    private TextField search = new TextField("");
    private Label lb_pacas = new Label("");
    private Label lb_unidades = new Label("");

    public ProductoContainer() {

        Resources res = null;
        try {
            res = Datos.getInstance().getRes();
            setLayout(new BoxLayout(BoxLayout.Y_AXIS));
            search.setConstraint(TextField.NUMERIC);
            search.setInputModeOrder(new String[]{"123"});
            search.setUseSoftkeys(false);
            bt_buscar = new Button(res.getImage("search.png"));
            bt_buscar.setAlignment(Button.CENTER);
            bt_buscar.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    cargaDatos();
                }

                private void cargaDatos() {
                    Persistencia p = null;
                    int ctrPacas = 0;
                    int ctrUnidades = 0;
                    try {
                         p = new Persistencia(InvMovilData.PERS_CAPTURA);
                        RecordEnumeration re = p.getRecordStore().enumerateRecords(null, null, false);
                        while (re.hasNextElement()) {
                            PersisCaptura pp = new PersisCaptura();
                            pp.resurrect(re.nextRecord());
                            if (search.getText().equals(pp.getCodigoProducto())) {
                                ctrPacas += pp.getPacas();
                                ctrUnidades += pp.getUnidades();

                            }
                        }                        
                        lb_pacas.setText(Integer.toString(ctrPacas));
                        lb_unidades.setText(Integer.toString(ctrUnidades));


                    } catch (Exception ex) {
                        ex.printStackTrace();
                        lb_unidades.setText("ERROR");
                        lb_pacas.setText("ERROR");
                    } finally {
                        try {
                            if (p != null) {
                                p.close();
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }

              
            });
            Container c1 = new Container(new BorderLayout());
            Dimension d = bt_buscar.getPreferredSize();
            d.setWidth(Math.max(d.getWidth(), 40));
            bt_buscar.setPreferredSize(d);
            c1.addComponent(BorderLayout.CENTER, search);
            c1.addComponent(BorderLayout.EAST, bt_buscar);
            addComponent(c1);
            Container largest = createPair("Pacas: ", lb_pacas, 30);
            int largestW = largest.getComponentAt(0).getPreferredW();
            addComponent(largest);
            largest = createPair("Unidades: ", lb_unidades, largestW);
            addComponent(largest);

        } catch (Exception e) {
            Datos.getInstance().addLogs(e.getMessage());
        } finally {
            res = null;
        }

    }

    protected Container createPair(String label, Component c, int minWidth) {
        Container pair = new Container(new BorderLayout());
        Label l = new Label(label);
        Dimension d = l.getPreferredSize();
        d.setWidth(Math.max(d.getWidth(), minWidth));
        l.setPreferredSize(d);
        //l.getStyle().setBgTransparency(100);
        pair.addComponent(BorderLayout.WEST, l);
        pair.addComponent(BorderLayout.CENTER, c);
        return pair;
    }
}
